package com.aqgj.Organization.web;

import com.aqgj.Organization.bean.Organization;
import com.aqgj.Organization.service.OrganizationManager;
import com.aqgj.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;


@Controller
@RequestMapping("organization")
public class OrganizationAction extends BaseController{

    @Autowired
    private OrganizationManager organizationManager;

    /**
     * 部门资源树
     *
     * @return
     */
    @PostMapping(value = "/tree")
    @ResponseBody
    public Object tree() {
        return renderSuccess(organizationManager.selectTree());
    }

    /**
     * 部门管理列表
     *
     * @return
     */
    @RequestMapping("/treeGrid")
    @ResponseBody
    public Object treeGrid() {
        return renderSuccess(organizationManager.selectTreeGrid());
    }


    /**
     * 添加部门
     *
     * @param organization
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Organization organization) {
        organization.setCreateTime(new Date());
        Boolean isTrue = organizationManager.insertOrganization(organization);
        if(isTrue){
            return renderSuccess("添加成功！");
        }else{
            return renderSuccess("添加失败！");
        }
    }

    /**
     * 编辑部门页
     *
     * @param
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    @ResponseBody
    public Object editPage(String id) {
        Organization organization = organizationManager.getOrganizationById(id);
        return renderSuccess(organization);
    }

    /**
     * 编辑部门
     *
     * @param organization
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(Organization organization) {
        organizationManager.updateOrganizationById(organization);
        return renderSuccess("编辑成功！");
    }

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        organizationManager.deleteOrganizationById(id);
        return renderSuccess("删除成功！");
    }


}